﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using cndp.Model.IModel;

namespace cndp.Model
{
    /// <summary>
    /// DataCheck - DataCheckklassen innehåller metoder för att kontrollera att indata är på rätt form. Att t.ex. ett namn innehåller bara bokstäver och inga siffor m.m.
    /// </summary>
    public struct Datacheck : IDataCheck
    {
        #region CheckIfContainNumbers

        public bool CheckIfContainNumbers(string name)
        {
            //Skickar tbx true om name innehåller en siffra
            var reg = new Regex(@"^[A-Za-z-é öÖäÄåÅ]+$");
            return !(reg.IsMatch(name));
        }

        #endregion

        #region CheckIfBirthday

        public bool CheckIfBirthDay(string b)
        {
            var length = b.Length;
            var position = 0;

            if (length == 13)
            {
                foreach (var c in b)
                {
                    if ((c >= '0' && c <= '9') && (position != 8))
                    {
                    }

                    else if ((position == 8) && (c == '-'))
                    {
                    }

                    else
                        return false;
                    position++;
                }
            }
            else
            {
                return false;
            }
            return true;
        }

        #endregion

        #region CheckIfInteger

        public bool CheckIfInteger(string i)
        {
            if (i == "")
                return false;
            var reg = new Regex(@"^[0-9]*$");
            return reg.IsMatch(i);
        }

        #endregion

        #region CheckIfIntegerAndNotNegativ
        public bool CheckIfIntegerAndNotNegative(string i)
        {
            if (CheckIfInteger(i))
            {
                return Convert.ToInt32(i) > -1;
            }
            return false;
        }

        #endregion


        #region CheckIfDate

        public bool CheckIfDate(string b)
        {
            if (b == "")
                return false;
            DateTime d;
            return DateTime.TryParseExact(b, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
        }
        #endregion

        #region CheckIfDateAndTime
        public bool CheckIfDateAndTime(string b)
        {
            if (b == "")
                return false;
            DateTime d;
            return DateTime.TryParseExact(b, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
        }
        #endregion

        #region CheckJumpExists
        public bool CheckJumpExists(int jumpId, string style,double JumpHeight,JumpAppendix jumpAppendix)
        {
            if (JumpHeight == 1)
            {
                if (style == "straight")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.OneMeter.Straight != 0); }
                    );
                    if (jumpFound == null) { return false; }

                }
                else if (style == "tuck")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.OneMeter.Tuck != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "pike")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.OneMeter.Pike != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "free")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.OneMeter.Free != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                
            }
            else if (JumpHeight == 3)
            {
                if (style == "straight")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.ThreeMeter.Straight != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "tuck")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.ThreeMeter.Tuck != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "pike")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.ThreeMeter.Pike != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "free")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.ThreeMeter.Free != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
            }
            else if (JumpHeight == 5)
            {
                if (style == "straight")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.FiveMeter.Straight != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "tuck")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.FiveMeter.Tuck != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "pike")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.FiveMeter.Pike != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "free")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.FiveMeter.Free != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
            }
            else if (JumpHeight == 7.5)
            {
                if (style == "straight")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.SevenMeter.Straight != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "tuck")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.SevenMeter.Tuck != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "pike")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.SevenMeter.Pike != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "free")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.SevenMeter.Free != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
            }
            else if (JumpHeight == 10)
            {
                if (style == "straight")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.TenMeter.Straight != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "tuck")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.TenMeter.Tuck != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "pike")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.TenMeter.Pike != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
                else if (style == "free")
                {
                    var jumpFound = jumpAppendix.JumpAppendixList.Find(
                    delegate(Jump j) { return ((j.JumpId == jumpId) && j.TenMeter.Free != 0); }
                    );
                    if (jumpFound == null) { return false; }
                }
            }
            return true;
        }
        #endregion

    }
}